﻿using KZ.CustomControl;
using KZ.Utilities;
using System;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows.Forms;

namespace KZ.Express
{
    public class Global
    {
        public static void GenerateNumber(ref DataTable table)
        {
            int count = table.Rows.Count;
            for (int i = 0; i < count; i++)
                table.Rows[i]["Number"] = i + 1;
        }

        public static void LoadComboBox(ComboBox cmb, DataTable source)
        {
            object temp = cmb.SelectedValue;
            cmb.DataSource = null;
            cmb.DataSource = source;
            try
            {
                if (temp != null)
                    cmb.SelectedValue = temp;
            }
            catch
            {
                cmb.SelectedIndex = -1;
            }
        }

        public static void LoadComboBox(MultiColumnComboBox cmb, DataTable source)
        {
            object temp = cmb.SelectedValue;
            cmb.Source = source;
            try
            {
                if (temp != null)
                    cmb.SelectedValue = temp;
            }
            catch
            {
                cmb.SelectedIndex = -1;
            }
        }

        public static void SetTabText(string text)
        {
            Form frm = Application.OpenForms["MainView"];
            TabControl tbc = frm.Controls["tbcMain"] as TabControl;
            tbc.SelectedTab.Text = text;
        }

        public static string RemoveNamespace(string name)
        {
            return name.Split('.').Last();
        }

        public static string GetStringFromDataGridView(DataGridView dtg, int colIndex)
        {
            StringBuilder content = new StringBuilder();
            foreach (DataGridViewRow row in dtg.SelectedRows)
            {
                content.Append("," + row.Cells[colIndex].Value);
            }
            if (dtg.SelectedRows.Count > 0) { content = content.Remove(0, 1); }

            return content.ToString();
        }

        public static FormBase CreateInstance(string name)
        {
            try
            {
                Type type = Type.GetType(name);
                if (type == null) { throw new BusinessLogicException("BLM_ERR_FORM_NOTEXIST"); }

                return Activator.CreateInstance(type) as FormBase;
            }
            catch (BusinessLogicException ex)
            {
                Log.LogEx(ex.Message, Log.LogType.Fatal, ex);
                return null;
            }
        }

        public static DialogResult ShowMessageConfirm(string message)
        {
            ResourceManager resource = new ResourceManager("KZ.Express.KZ.Express.Resource", typeof(KZ_Express_Resource).Assembly);
            string msg = resource.GetString(message);

            return MessageBox.Show(String.IsNullOrWhiteSpace(msg) ? message : msg, String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }

        public static void SetDoubleBuffered(Control control)
        {
            // set instance non-public property with name "DoubleBuffered" to true
            typeof(Control).InvokeMember("DoubleBuffered",
                BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                null, control, new object[] { true });
        }
    }
}